package com.pengxun.manager.service.system.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pengxun.manager.dao.system.RoleDao;
import com.pengxun.manager.enums.UserTypeEnums;
import com.pengxun.manager.model.system.Role;
import com.pengxun.manager.model.system.RoleMenu;
import com.pengxun.manager.model.system.UserRole;
import com.pengxun.manager.model.system.Userinfo;
import com.pengxun.manager.service.system.RoleMenuService;
import com.pengxun.manager.service.system.RoleService;
import com.pengxun.manager.service.system.UserRoleService;
import com.pengxun.manager.vo.MenuCodeTypeVO;
import com.pengxun.manager.vo.RoleParam;
import com.pengxun.manager.vo.RolePermissionVO;
import com.szeastroc.common.exception.EastrocException;
import com.szeastroc.common.jdbc.page.Page;
import com.szeastroc.common.utils.CodeUtils;

@Service
public class RoleServiceImpl implements RoleService {

	@Resource
	private RoleDao roleDao;
	
	@Resource
	private RoleMenuService roleMenuService;
	
	@Resource
	private UserRoleService userRoleService;

	@Override
	@Transactional
	public void insert(Role role) {
		Role lastRole = selectLastRole();
		if(null == lastRole){
			role.setRoleCode(CodeUtils.generateCode("roleCode", 6, "roleCode000000"));
		}else{
			role.setRoleCode(CodeUtils.generateCode("roleCode", 6, lastRole.getRoleCode()));
		}
		roleDao.insert(role);
		//如果设置了权限则新增权限
		RolePermissionVO rolePermissionVO = role.getRolePermission();
		if(null != rolePermissionVO){
			List<RoleMenu> roleMenus = new ArrayList<RoleMenu>();
			RoleMenu roleMenu = null;
			for (MenuCodeTypeVO menuCodeTypeVO : rolePermissionVO.getPermissions()) {
				roleMenu = new RoleMenu();
				if (menuCodeTypeVO.getType() == 0) {
					roleMenu.setMenuCode(menuCodeTypeVO.getCode());
				} else if (menuCodeTypeVO.getType() == 1) {
					roleMenu.setPermissionCode(menuCodeTypeVO.getCode());
				}
				roleMenu.setIsMenuPermission(menuCodeTypeVO.getType());
				roleMenu.setRoleCode(rolePermissionVO.getRoleCode());
				roleMenus.add(roleMenu);
			}
			roleMenuService.batchInsert(roleMenus);
		}
	}

	@Override
	public int update(Role role) {
		return roleDao.update(role);
	}

	@Override
	public int delete(Integer id) {
		return roleDao.delete(id);
	}

	@Override
	public Page findByPage(RoleParam role) {
		List<Role> roles = roleDao.findByPage(role);
		role.setData(roles);
		Page page = new Page();
		BeanUtils.copyProperties(role, page);
		page.setData(roles);
		return page;
	}
	
	@Override
	public List<Role> findAll(){
		return roleDao.findAll();
	}

	@Transactional
	public void deleteByRoleCode(String roleCode) {
		try{
			roleDao.deleteByRoleCode(roleCode);
			roleMenuService.deleteByRoleCode(roleCode);
			userRoleService.deleteByroleCode(roleCode);
		}catch(Exception e){
			throw new EastrocException("删除角色失败");
		}
	}
	
	@Override
	public Role findByRoleCode(String roleCode){
		return roleDao.findByRoleCode(roleCode);
	}

	@Override
	public List<Role> findByRoleCodes(List<String> roleCodes){
		return roleDao.findByRoleCodes(roleCodes);
	}

	
	@Override
	public Role selectLastRole(){
		return roleDao.selectLastRole();
	}

	@Override
	public List<Role> findRoleByCurrentUser(Userinfo userinfo) {
		String userType = userinfo.getUserType();
		List<Role> list = null;
		if(UserTypeEnums.PLATFORM_ADMIN.getType().equals(userType)) {
			list = roleDao.findAll();
			return list;
		}
		List<UserRole> userRoleList = userRoleService.findByUserId(userinfo.getUserId());
		List<String> roleCodes = new ArrayList<String>();
		for (UserRole userRole : userRoleList) {
			roleCodes.add(userRole.getRoleCode());
		}
		list = roleDao.findByRoleCodes(roleCodes);
		return list;
	}

}
